package com.penjin.android.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;

import com.penjin.android.constants.PenjinConstants;
import com.penjin.android.domain.JPushData;
import com.penjin.android.domain.PenjinCompany;
import com.penjin.android.domain.PenjinStaff;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.receiver.JPushConstants;
import com.penjin.android.utils.BitmapUtils;
import com.penjin.android.utils.JsonUtils;
import com.squareup.picasso.Picasso;

import java.io.File;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by maotiancai on 2015/12/19.
 */
public class UserService {

    public static String avatarPath = "";//用户头像缓存地址
    public static String qrPath = "";//用户二维码名片缓存地址

    private PenjinUser user = null;
    private Context context;
    private SharedPreferences sharedPreferences;
    private static UserService instance;

    private UserService(Context context) {
        this.context = context.getApplicationContext();
        sharedPreferences = this.context.getSharedPreferences("PenjinUser", 1);
    }

    public static UserService getInstance(Context context) {
        synchronized (UserService.class) {
            if (instance == null) {
                instance = new UserService(context);
            }
        }
        return instance;
    }

    public File getUserAvatarFile() throws Exception {
        return new File(Environment.getExternalStorageDirectory()
                + File.separator + PenjinConstants.ROOT +
                File.separator + PenjinConstants.USER_DIR +
                File.separator + PenjinConstants.AVATAR_FILE_NAME);
    }

    /**
     * 获取用户头像图片
     *
     * @return
     */
    public Bitmap getUserAvatar() throws Exception {
        String userDir = this.getCurrentUser().getPhone();
        return BitmapUtils.readImageBitmap(new File(Environment.getExternalStorageDirectory()
                + File.separator + PenjinConstants.ROOT +
                File.separator + userDir +
                File.separator + PenjinConstants.AVATAR_FILE_NAME));
    }

    /**
     * 保存用户头像
     *
     * @param
     */
    public void saveUserAvatar(Bitmap avatar) throws Exception {
        BitmapUtils.saveImageBitmap(new File(Environment.getExternalStorageDirectory()
                + File.separator + PenjinConstants.ROOT +
                File.separator + PenjinConstants.USER_DIR +
                File.separator + PenjinConstants.AVATAR_FILE_NAME), avatar);
    }

    /**
     * 获得当前用户信息
     *
     * @return
     */
    public PenjinUser getCurrentUser() {
        String userPhone = sharedPreferences.getString("phone", null);
        if (userPhone == null) {
            return null;
        }
        user = new PenjinUser();
        user.setUsername(sharedPreferences.getString("username", null));
        user.setPassword(sharedPreferences.getString("password", null));
        user.setEmail(sharedPreferences.getString("email", null));
        user.setPhone(sharedPreferences.getString("phone", null));
        user.setAddress(sharedPreferences.getString("address", null));
        user.setRegion(sharedPreferences.getString("region", null));
        user.setChatId(sharedPreferences.getString("chatId", null));
        user.setCompanyId(sharedPreferences.getString("companyId", null));
        user.setStaffNum(sharedPreferences.getString("staffNum", null));
        user.setSex(sharedPreferences.getString("sex", null));
        user.setQianming(sharedPreferences.getString("qianming", null));
        user.setTouxiang(sharedPreferences.getString("touxiang", null));
        return user;
    }

    /**
     * 获得当前用户公司信息
     *
     * @return
     */
    public PenjinCompany getCurrentCompany() {
        PenjinCompany penjinCompany = new PenjinCompany();
        penjinCompany.setCompanyName(sharedPreferences.getString("companyName", null));
        penjinCompany.setUrl(sharedPreferences.getString("url", null));
        penjinCompany.setAddress(sharedPreferences.getString("address", null));
        penjinCompany.setEmail(sharedPreferences.getString("email", null));
        penjinCompany.setTelephone(sharedPreferences.getString("telephone", null));
        penjinCompany.setCompanyId(sharedPreferences.getString("companyId", null));
        penjinCompany.setCompanyPersonNumber(sharedPreferences.getString("companyPersonNumber", null));
        return penjinCompany;
    }

    /**
     * 缓存当前用户信息
     *
     * @param user
     */
    public void saveUser(PenjinUser user) {
        this.user = null;
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putString("userId", user.getUserId());
        editor.putString("username", user.getUsername());//昵称
        editor.putString("password", user.getPassword());
        editor.putString("email", user.getEmail());
        editor.putString("phone", user.getPhone());
        editor.putString("address", user.getAddress());
        editor.putString("region", user.getRegion());
        editor.putString("chatId", user.getChatId());
        editor.putString("companyId", user.getCompanyId());
        editor.putString("staffNum", user.getStaffNum());
        editor.putString("sex", user.getSex());
        editor.putString("touxiang", user.getTouxiang());
        editor.putString("qianming", user.getQianming());
        editor.commit();
    }

    public void saveCompany(PenjinCompany company) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("address", company.getAddress());
        editor.putString("url", company.getUrl());
        editor.putString("companyPersonNumber", company.getCompanyPersonNumber());
        editor.putString("email", company.getEmail());
        editor.putString("telephone", company.getTelephone());
        editor.putString("companyName", company.getCompanyName());
        editor.putString("companyId", company.getCompanyId());
        editor.commit();
    }

    public void clearUser() {
        this.user = null;
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putString("phone", null);
        editor.commit();
    }

    /**
     * @return 当前用户的状态，-1：未登录;0:登录但是未绑定企业；1：登录且绑定了企业
     */
    public int getCurrentUserState() {
        PenjinUser user = getCurrentUser();
        if (user == null) {
            return -1;
        }
        if (JsonUtils.isJsonNull(user.getCompanyId())) {
            return 0;
        }
        return 1;
    }

    /***
     * 缓存最新推送消息,区分平台推送公司推送
     *
     * @param bundle
     * @param TAG
     */
    public void setNewPush(Bundle bundle, String TAG) {

        SharedPreferences.Editor editor = sharedPreferences.edit();

        if (bundle.getString(JPushInterface.EXTRA_MESSAGE) != null) {

            editor.putString(TAG + JPushInterface.EXTRA_TITLE, bundle.getString(JPushInterface.EXTRA_TITLE));
            editor.putString(TAG + JPushInterface.EXTRA_MESSAGE, bundle.getString(JPushInterface.EXTRA_MESSAGE));
            editor.putString(TAG + JPushInterface.EXTRA_EXTRA, bundle.getString(JPushInterface.EXTRA_EXTRA));

        }
        if (bundle.getString(JPushInterface.EXTRA_ALERT) != null) {

            editor.putString(TAG + JPushInterface.EXTRA_NOTIFICATION_TITLE, bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE));
            editor.putString(TAG + JPushInterface.EXTRA_ALERT, bundle.getString(JPushInterface.EXTRA_ALERT));
            editor.putString(TAG + JPushInterface.EXTRA_EXTRA, bundle.getString(JPushInterface.EXTRA_EXTRA));

        }

        if (TAG == JPushConstants.Zhang_Liang) {

            editor.putInt(TAG + "num", bundle.getInt(TAG + "num"));

        }

        editor.commit();

    }

    /***
     * 获取最新推送消息,区分平台推送公司推送
     *
     * @param TAG
     * @return
     */
    public JPushData getNewPush(String TAG) {

        JPushData jPushData = new JPushData();

        jPushData.setMessageTitle(sharedPreferences.getString(TAG + JPushInterface.EXTRA_TITLE, null));
        jPushData.setMessage(sharedPreferences.getString(TAG + JPushInterface.EXTRA_MESSAGE, null));
        jPushData.setAlertTitle(sharedPreferences.getString(TAG + JPushInterface.EXTRA_NOTIFICATION_TITLE, null));
        jPushData.setAlert(sharedPreferences.getString(TAG + JPushInterface.EXTRA_ALERT, null));
        jPushData.setExtra(sharedPreferences.getString(TAG + JPushInterface.EXTRA_EXTRA, null));
        jPushData.setNum(sharedPreferences.getInt(TAG + "num", 0));

        return jPushData;

    }

    /***
     * 缓存小秘书详细条目未读消息数
     *
     * @param TAG
     * @param num
     */
    public void setSecretaryNum(String TAG, int num) {

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putInt(TAG + "num", num);

        editor.commit();

    }

    /***
     * 获取小秘书详细条目未读消息数
     *
     * @param TAG
     * @return
     */
    public int getSecretaryNum(String TAG) {

        return sharedPreferences.getInt(TAG + "num", 0);

    }

    public PenjinStaff getCurrentStaff() {
        PenjinStaff penjinStaff = new PenjinStaff();
        penjinStaff.setStaffNumber(sharedPreferences.getString("staffNumber", null));
        penjinStaff.setDepartment(sharedPreferences.getString("department", null));
        penjinStaff.setPosition(sharedPreferences.getString("position", null));
        penjinStaff.setInOffice(sharedPreferences.getString("inOffice", null));
        penjinStaff.setRealName(sharedPreferences.getString("realName", null));
        return penjinStaff;
    }

    public void saveCurrentStaff(PenjinStaff penjinStaff) {
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putString("staffNumber", penjinStaff.getStaffNumber());
        editor.putString("department", penjinStaff.getDepartment());
        editor.putString("position", penjinStaff.getPosition());
        editor.putString("inOffice", penjinStaff.getInOffice());
        editor.putString("realName", penjinStaff.getRealName());
        editor.commit();
    }
}
